package com.sun.officeoa.servlet;

import com.sun.officeoa.bean.Employee;
import com.sun.officeoa.param.WebContextParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 */
public class LoginIntercepter extends  HandlerInterceptorAdapter  {

    public static final Logger logger = LoggerFactory.getLogger(LoginIntercepter.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        logger.info(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用（Controller方法调用之前）");
        //获取session
        HttpSession session = request.getSession(true);
        logger.info("----进入登录拦截器--url："+request.getServletPath()+"-----");
        Employee es = (Employee) session.getAttribute("loginUser");
        //看session里是否有保存的登陆用户
        if (es == null) {
            response.sendRedirect("/office-oa/login");
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }


}
